﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FileSynchronizer
{
    public class FileSyncApplication : ApplicationContext
    {
        private System.ComponentModel.IContainer _components;
        private NotifyIcon _notifyIcon;
        private ContextMenuStrip _contextMenu;
        private ToolStripMenuItem _syncNowMenuItem;
        private ToolStripMenuItem _configMenuItem;
        private ToolStripMenuItem _quitMenuItem;

        public FileSyncApplication()
        {
            _components = new System.ComponentModel.Container();

            _notifyIcon = new NotifyIcon(this._components);
            _notifyIcon.Icon = new System.Drawing.Icon("hdd.ico");
            _notifyIcon.Text = "File Sync Synchronizer";
            _notifyIcon.Visible = true;

            _contextMenu = new ContextMenuStrip();
            _syncNowMenuItem = new ToolStripMenuItem();
            _configMenuItem = new ToolStripMenuItem();
            _quitMenuItem = new ToolStripMenuItem();

            _notifyIcon.ContextMenuStrip = _contextMenu;

            _syncNowMenuItem.Text = "Sync now!";
            _syncNowMenuItem.Click += new EventHandler(_syncNowMenuItem_Click);
            _contextMenu.Items.Add(_syncNowMenuItem);

            _configMenuItem.Text = "Settings";
            _configMenuItem.Click += new EventHandler(_configMenuItem_Click);
            _contextMenu.Items.Add(_configMenuItem);

            _quitMenuItem.Text = "Quit";
            _quitMenuItem.Click += new EventHandler(_exitMenuItem_Click);
            _contextMenu.Items.Add(_quitMenuItem);
        }

        #region Event handlers

        void _syncNowMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                SyncAgent.Instance.Synchronize();
            }
            catch(SynchronizationException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        void _configMenuItem_Click(object sender, EventArgs e)
        {
            SettingsView mainForm = new SettingsView();
            mainForm.ShowDialog();
        }

        void _exitMenuItem_Click(object sender, EventArgs e)
        {
            ExitThreadCore();
        }

        #endregion Event handlers
    }
}